package com.service.weChatWifiLogin.service.impl;

import com.service.config.constant.Constant;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import com.service.config.utils.ScanFilesWithRecursionUtils;

import com.service.weChatWifiLogin.dao.ShopInfoDao;
import com.service.weChatWifiLogin.dao.ShopInfoReviewDao;
import com.service.weChatWifiLogin.dao.VFilePhysicsInfoDao;
import com.service.weChatWifiLogin.entity.ShopInfo;
import com.service.weChatWifiLogin.entity.ShopInfoReview;
import com.service.weChatWifiLogin.entity.VFilePhysicsInfo;
import com.service.weChatWifiLogin.enums.ReviewStatusEnum;
import com.service.weChatWifiLogin.service.ShopInfoService;
import com.service.weChatWifiLogin.service.VFilePhysicsInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 物理文件消息表(VFilePhysicsInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-05-18 19:56:30
 */
@Service("vFilePhysicsInfoService")
public class VFilePhysicsInfoServiceImpl implements VFilePhysicsInfoService {
    @Resource
    private VFilePhysicsInfoDao vFilePhysicsInfoDao;
    @Value("${spring.fileSeverIp}")
    private String systemIp;

    @Value("${spring.fileSeverDomain}")
    private String systemDomain;

    @Value("${spring.fileDic}")
    private String systemDic;

    @Value("${spring.fileDicDomain}")
    private String systemDicDomain;

    @Value("${spring.imageUrl}")
    private String imageUrl;
    @Resource
    private ShopInfoReviewDao shopInfoReviewDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VFilePhysicsInfo queryById(String id) {
        return this.vFilePhysicsInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<VFilePhysicsInfo> queryAllByLimit(int offset, int limit) {
        return this.vFilePhysicsInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param vFilePhysicsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public VFilePhysicsInfo insert(VFilePhysicsInfo vFilePhysicsInfo) {
        this.vFilePhysicsInfoDao.insert(vFilePhysicsInfo);
        return vFilePhysicsInfo;
    }

    /**
     * 修改数据
     *
     * @param vFilePhysicsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public VFilePhysicsInfo update(VFilePhysicsInfo vFilePhysicsInfo) {
        this.vFilePhysicsInfoDao.update(vFilePhysicsInfo);
        return this.queryById(vFilePhysicsInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.vFilePhysicsInfoDao.deleteById(id) > 0;
    }

    @Override
    public Result uploadServiceLogo(MultipartFile file, String programmeId, boolean isLogo) throws Exception {
        Map duplicate = duplicate(file);
        String url = "";
        VFilePhysicsInfo vFilePhysicsInfo = ModelMapperUtil.strictMap(duplicate.get("vFilePhysicsInfo"), VFilePhysicsInfo.class);
        Boolean.parseBoolean(duplicate.get("boolean").toString());
        if (programmeId != null && !programmeId.equals("")) {
            uploadLogo(vFilePhysicsInfo, file);
            VFilePhysicsInfo vFilePhysicsInfo1 = vFilePhysicsInfoDao.queryById(vFilePhysicsInfo.getId());
            // 上传图片绑定逻辑
            ShopInfo shopInfo = new ShopInfo();
            shopInfo.setId(programmeId);
            if (!isLogo) {
                shopInfo.setLogoType(0);
                shopInfo.setLogoId(vFilePhysicsInfo.getId());
                if (vFilePhysicsInfo1 != null) {
                    url = vFilePhysicsInfo.getFileseverdomain() + vFilePhysicsInfo.getFiledicdomain()
                            + "/" + vFilePhysicsInfo.getFilename();
                }
            } else {
                shopInfo.setMarketingType(0);
                shopInfo.setMarketingId(vFilePhysicsInfo.getId());
                if (vFilePhysicsInfo1 != null) {
                    url = vFilePhysicsInfo.getFileseverdomain() + vFilePhysicsInfo.getFiledicdomain()
                            + "/" + vFilePhysicsInfo.getFilename();
                }
            }
            ShopInfoReview shopInfoReview = shopInfoReviewDao.queryById(programmeId); // 上传图片需审核
            if (shopInfoReview != null) {
                ShopInfoReview shopInfoReview1 = ModelMapperUtil.strictMap(shopInfo, ShopInfoReview.class);
                shopInfoReview1.setReviewStatus(Constant.REVIEW_WAIT); //  0 -- 待审核
                shopInfoReview1.setReviewStatusDesc("待审核");
                shopInfoReview1.setUpdateTime(new Date());
                shopInfoReviewDao.update(shopInfoReview1); // 提交审核
                return Result.ok().setData(url);
            } else {
                ShopInfoReview shopInfoReview1 = ModelMapperUtil.strictMap(shopInfo, ShopInfoReview.class);
                shopInfoReview1.setReviewStatus(0);//  0 -- 待审核
                shopInfoReview1.setReviewStatusDesc("待审核");
                shopInfoReview1.setUpdateTime(new Date());
                shopInfoReview1.setAddTime(new Date());
                shopInfoReviewDao.insert(shopInfoReview1); // 提交审核
                return Result.ok().setData(url);
            }
        }
        HashMap map = new HashMap();
        map.put("errorMsg","店铺不存在");
        map.put("code",10001);
        return  Result.ok().setData(map);
    }
    /**
     * 上传logo
     * @param vFilePhysicsInfo
     * @throws Exception
     */
    @Transactional
    public void uploadLogo(VFilePhysicsInfo vFilePhysicsInfo, MultipartFile file) throws IOException {
        vFilePhysicsInfo.setFiledic(imageUrl);
        vFilePhysicsInfo.setFiledicdomain("wifilink/logo");
        File file1 = createFile(vFilePhysicsInfo.getFiledic() +"/"+ vFilePhysicsInfo.getFilename());
        createFile(file,file1);
        vFilePhysicsInfo.setFilemd5(ScanFilesWithRecursionUtils.getJdkFileMD5(file1.getPath()));
        vFilePhysicsInfoDao.insert(vFilePhysicsInfo);
    }
    private File createFile(String s) {
        File file = new File(s);
        return file;
    }
    private void createFile(MultipartFile file,File file1) throws IOException {
        file.transferTo(file1);
    }
    /**
     * 抽取共同代码
     * @param file
     * @return
     * @throws Exception
     */
    private Map duplicate(MultipartFile file) throws Exception {
        Map map = new HashMap();
        VFilePhysicsInfo vFilePhysicsInfo = new VFilePhysicsInfo();
        vFilePhysicsInfo.setId(IdUtil.getStringId());
        String originalFilename = file.getOriginalFilename();// 文件原始名称
        vFilePhysicsInfo.setFilerealname(originalFilename); // 设置文件真实名字
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i + 1);
        boolean b1 = judgeFileType(substring);// 判断文件类型
        vFilePhysicsInfo.setFiletype(substring);
        vFilePhysicsInfo.setUpdtime(new Date());
        vFilePhysicsInfo.setAddtime(new Date());
        vFilePhysicsInfo.setFilename(UUID.randomUUID().toString().replace("-","")+"."+substring); // 对文件重命名
        vFilePhysicsInfo.setFileseverip(systemIp);// 服务器ip
        vFilePhysicsInfo.setFileseverdomain(systemDomain); // 服务器域名
        vFilePhysicsInfo.setUpdtime(new Date());
        vFilePhysicsInfo.setStatus("1");
        vFilePhysicsInfo.setDelflag("0");
        map.put("vFilePhysicsInfo",vFilePhysicsInfo);
        map.put("boolean",b1);
        return map;
    }
    /**
     * 判断文件类型
     * @param substring
     * @return
     */
    private boolean judgeFileType(String substring) {
        if (substring.equals("gif")) {
            return true;
        }
        if (substring.equals("jpg")) {
            return true;
        }
        if (substring.equals("jpeg")) {
            return true;
        }
        if (substring.equals("png")) {
            return true;
        }
        if (substring.equals("GIF")) {
            return true;
        }
        if (substring.equals("JPG")) {
            return true;
        }
        if (substring.equals("JPEG")) {
            return true;
        }
        if (substring.equals("PNG")) {
            return true;
        }
        return false;
    }
}
